<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      




      <!-- <el-form-item label="所属仓库" prop="warehouseId">
        <el-select @click="selectware" v-model="ware" placeholder="请选择仓库" clearable>
          <el-option @click="seachware(dict.warehouseId)" v-for="dict in options"
            :key="'key-' + dict.warehouseId" :label="dict.warehouseName"
            :value="dict.warehouseId" ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属库区" prop="areaId">
         <el-select v-model="area" placeholder="请选择库区" clearable>
            <el-option @click="" v-for="dict in areas" :key="'key-' + dict.areaNo" :label="dict.areaName"
              :value="dict.areaNo" ></el-option>
          </el-select>
      </el-form-item> -->




      
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-select @click="selectware" v-model="queryParams.warehouseId" placeholder="请选择仓库"  @keyup.enter="handleQuery"
        clearable>
          <el-option @click="seachware(dict.warehouseId)" v-for="dict in options"
            :key="'key-' + dict.warehouseId" :label="dict.warehouseName"
            :value="dict.warehouseId" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属库区" prop="areaId">
         <el-select v-model="queryParams.areaId" placeholder="请选择库区"  @keyup.enter="handleQuery"
         clearable>
            <el-option @click="" v-for="dict in areas" :key="'key-' + dict.areaNo" :label="dict.areaName"
              :value="dict.areaNo" ></el-option>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    
    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      
      
      <el-table-column label="所属仓库" align="center" prop="warehouse_name" />
      <el-table-column label="所属库区" align="center" prop="area_name" />
      <el-table-column label="物料名称" align="center" prop="item_name" />
      <el-table-column label="库存" align="center" prop="sum" />
      <el-table-column label="物料备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:inventory:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:inventory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存对话框 -->
    <!-- <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="inventoryRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料ID" prop="itemId">
          <el-input v-model="form.itemId" placeholder="请输入物料ID" />
        </el-form-item>
        <el-form-item label="库存" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标识" />
        </el-form-item>
        <el-form-item label="所属仓库" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入所属仓库" />
        </el-form-item>
        <el-form-item label="所属库区" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入所属库区" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog> -->
  </div>
</template>

<script setup name="Inventory">
import {listInventory, getInventory, delInventory, addInventory, updateInventory } from "@/api/wms/inventory";
import {listWarehouse} from "@/api/wms/warehouse";
import {selectList} from "@/api/wms/area";
const options=ref([]);
const areainList = ref([]);
const ware = ref();
const area = ref();
const areas = ref([]);

const { proxy } = getCurrentInstance();
const inventoryList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemId: null,
    quantity: null,
    warehouseId: null,
    areaId: null,
    warehouseName:null,
    areaName:null,
    typeName:null,
  },
  rules: {
    itemId: [
      { required: true, message: "物料ID不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标识不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询库存列表 */
function getList() {
  loading.value = true;
  listInventory(queryParams.value).then(response => {
    inventoryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    console.log(queryParams.value);
  });
 
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    itemId: null,
    quantity: null,
    remark: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    warehouseId: null,
    areaId: null,
    warehouseId: null,
    areaId: null,
    warehouseName:null,
    areaName:null,
    typeName:null,
  };
  proxy.resetForm("inventoryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加库存";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInventory(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改库存";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["inventoryRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInventory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInventory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除库存编号为"' + _ids + '"的数据项？').then(function() {
    return delInventory(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/inventory/export', {
    ...queryParams.value
  }, `inventory_${new Date().getTime()}.xlsx`)
}








//查询仓库
function selectware() {
  listWarehouse().then(response => {
    console.log(response.rows)
    options.value = response.rows
  })
}

//查询库区
function seachware(id) {
  selectList(id).then(response => {
    area.value = null
    areas.value = response.rows
  })
}



getList();
</script>
